package com.scau.yangsuiyu.estore.service;

import com.alibaba.fastjson.JSONObject;
import com.scau.yangsuiyu.estore.dababases.dao.BuyerDao;
import com.scau.yangsuiyu.estore.dababases.dao.SellerDao;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.Seller;
import com.scau.yangsuiyu.estore.dababases.entity.User;

/**
 * @author suiyu_yang
 * @description 实现登录的业务逻辑
 * @date 2019-05-20 10:24
 * @email suiyu_yang@163.com
 */

public class LoginService {

    public JSONObject login(JSONObject loginObject) {
        String loginRole = loginObject.getString("role");
        String loginKey = loginObject.getString("key");
        String password = loginObject.getString("password");

        if (loginRole.equals("buyer")) {
            return buyerLogin(loginKey, password);
        } else if (loginRole.equals("seller")) {
            return sellerLogin(loginKey, password);
        } else {
            return null;
        }
    }

    /**
     * 验证密码是否正确
     *
     * @param user
     * @param password
     * @return
     */
    private JSONObject verify(User user, String password) {
        JSONObject resultJson = new JSONObject();
        resultJson.put("user", null);
        resultJson.put("status", "failed");

        if (password.equals(user.getPsw())) {
            resultJson.put("status", "success");
            resultJson.put("result", "登录成功！");
            resultJson.put("user", user);
        } else {
            resultJson.put("result", "密码错误！");
        }
        return resultJson;
    }

    private JSONObject sellerLogin(String loginKey, String password) {

        //通过手机号码查找
        Seller seller = new SellerDao().getSellerByPhone(loginKey);
        if (seller != null) {
            return verify(seller, password);
        }

        //通过手机号码查找失败，通过电子邮箱查找
        seller = new SellerDao().getSellerByEmail(loginKey);
        if (seller != null) {
            return verify(seller, password);
        }

        //通过电子邮箱查找失败，通过用户名查找
        seller = new SellerDao().getSellerByUsername(loginKey);
        if (seller != null) {
            return verify(seller, password);
        }

        JSONObject resultJson = new JSONObject();
        resultJson.put("status", "failed");
        resultJson.put("user", null);
        resultJson.put("result", "用户不存在！");
        return resultJson;
    }

    private JSONObject buyerLogin(String loginKey, String password) {

        //通过手机号码查找
        Buyer buyer = new BuyerDao().getBuyerByPhone(loginKey);
        if (buyer != null) {
            return verify(buyer, password);
        }

        //通过手机号码查找失败，通过电子邮箱查找
        buyer = new BuyerDao().getBuyerByEmail(loginKey);
        if (buyer != null) {
            return verify(buyer, password);
        }

        //通过电子邮箱查找失败，通过用户名查找
        buyer = new BuyerDao().getBuyerByUsername(loginKey);
        if (buyer != null) {
            return verify(buyer, password);
        }

        JSONObject resultJson = new JSONObject();
        resultJson.put("status", "failed");
        resultJson.put("user", null);
        resultJson.put("result", "用户不存在！");
        return resultJson;
    }


}
